<template>
    <Dialog ref="dialogRef" :title="title" @confirm="submit" width="500">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="90px">
            <el-form-item label="厂家名称" prop="manufactorName">
                <el-input v-model="form.manufactorName" placeholder="请输入厂家名称" />
            </el-form-item>
            <el-form-item label="备注">
               <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
        </el-form>
    </Dialog>
</template>
<script setup>
import { getManufactorById, addManufactor, updateManufactor } from "@/api/simulation/manufacturers.js";
import { ref, getCurrentInstance, watch } from "vue";
const { proxy } = getCurrentInstance();
const emits = defineEmits(["confirm"]);
defineExpose({ open });
const dialogRef = ref(null);
const form = ref(null);
const formRef = ref(null);
const title = ref("厂家");
const rules = ref({manufactorName: [{ required: true, message: '厂家名称不能为空', trigger: 'blur' }]})

/**
 * 打开弹框
 */
async function open(params) {
    reset();
    // 打开弹框
    await dialogRef.value.open();
    if (params) {
        title.value = "编辑厂家";
        getData(params.id);
    } else {
        title.value = "新增厂家";
    }
}

/**
 * 获取数据
 * @param id 
 */
async function getData(id) {
    try {
        dialogRef.value.showLoading();
        const res = await getManufactorById({ id: id });
        form.value = res.data;
    } catch (e) {
        console.error(e);
    } finally {
        dialogRef.value.hideLoading();
    }
}



// 表单重置
function reset() {
    form.value = {
        manufactorName: null,
        remark:null
    }
}

/**
 * 确定
 */
function submit() {
    formRef.value.validate(async (valid) => {
        if (valid) {
            try {
                dialogRef.value.showLoading();
                if (form.value.id) {
                    await updateManufactor(form.value);
                } else {
                    await addManufactor(form.value);
                }
                proxy.$modal.msgSuccess("操作成功");
                emits("refresh");
                dialogRef.value.close();
            } catch (e) {
                console.error(e);
            } finally {
                dialogRef.value.hideLoading();
            }
        }
    });
}
</script>

  
  